var loaderFactory=function(obj){
	var loader=new Object();
	
	loader.pageNo=1;
	loader.ids=obj.ids;
	if(obj.pagerParameterName!="undifined"&&obj.pagerParameterName!=null){
		loader.pagerParameterName=obj.pagerParameterName;
	}
	loader.objs=loadObjs(loader.ids);
	loader.load=function(data){
		loader.write(data,0);
	};
	
	loader.getData=function(url){
	
		if(!loader.isRunning){
		    var script = document.createElement('script');
		    
		    if(loader.pagerParameterName!="undifined"&&loader.pagerParameterName!=null){
			    if(url.indexOf("?")==-1){
			    	url+="?"+loader.pagerParameterName+"="+loader.pageNo;
			    }else{
			    	url+="&"+loader.pagerParameterName+"="+loader.pageNo;
			    }
		    }
		    
		    log(url,"loader");

		    script.setAttribute('src', url);
		    document.getElementsByTagName('head')[0].appendChild(script);
	
		    loader.isRunning=true;
	    	loader.pageNo++;
		}
	};
	
	loader.writeItem=function(item){
		
		var shortestDiv = loader.findShortestDiv();
		var template = $("#template > ul > li").clone();
		
		var imgDiv = template.children(".img");
		var img_a = imgDiv.children("#clickUrl");
		img_a.attr("href","pin_item.htm");
		img_a.children(".item_img").attr("src",item.url);
		imgDiv.children(".desc").html(item.desc);
		
		var contentDiv = template.children(".content");
		contentDiv.children(".author_info").find("img").attr("src",item.user.head);
		contentDiv.children(".author_info").children(".desc").html(
			"由<a href='#'>"+item.user.nick+"</a>分享到<a href='#'>好图大全</a>"
		);
		
		var comment=item.commnet;
		var commentDiv = template.children(".content").children(".item_info_comment");
		var item=commentDiv.children(".item").clone();
		
		/*先清空，再加入*/
		commentDiv.empty();
		for(var i in comment){
			var itemClone=item.clone();
			itemClone.children(".pic").children("img").attr("src",comment[i].user.head);
			itemClone.children(".desc").html(comment[i].content);
			commentDiv.append(itemClone);
		}
		
		template.fadeTo(400,1);
		
		$(shortestDiv).find("ul").append(template);
	}
	
	loader.write=function(data,index){
		var item=data[index];
		loader.writeItem(item);
		index++;
		
		//结束
		if(index==data.length){
			loader.isRunning=false;
			return;
		}
			
		setTimeout(function(){loader.write(data,index);},100);
	};
	
	loader.findShortestDiv=function(){
        	var height=this.objs[0].offsetHeight;
        	var tempObj=this.objs[0];
        	for(var i=1;i<this.objs.length; i++){
        		var templength = this.objs[i].offsetHeight;
        		if(height>templength){
					height=templength;
					tempObj=this.objs[i];
        		}
        	}
        	return tempObj;
     };
     
     return loader;
}

function loadObjs(ids){
	var objs=new Array();
	for(var i=0;i<ids.length;i++){
		objs.push(document.getElementById(ids[i]));
	}
	return objs;
}        	
    		